/**
 * 请求处理
 * @param {} param0 
 * @returns 
 */
function $api({beanName, actionName, body, sync, ignoreCode, xhr}) {
  const params = {beanName, actionName, body: body || {}}
  const baseUrl = `${$env.getServerContextUrl()}api/opt.ptBaseRpcService/execute`
  if (xhr) {
    return new Promise((resolve, reject) => {
      let xhr = new XMLHttpRequest();
      xhr.withCredentials = false;
      xhr.open("post", baseUrl, false)
      // xhr.setRequestHeader('content-type', 'application/json;charset=UTF-8');
      //发送空内容请求
      xhr.onreadystatechange = () => {
        if (xhr.readyState == 4) {
          if (xhr.status == 200) {
            resolve(xhr.responseText)
          }else {
            ELEMENT.Message.error(xhr.statusText)
            reject(xhr.statusText)
          }
        }
      }
      xhr.send(JSON.stringify(params))
    })
  }
  if (sync) {
    let res = $ajax({
      url: baseUrl,
      jsonData: [params],
      sync: true,
    })
    if (res.code != 200) {
      ELEMENT.Message.error(res.message)
      return
    }
    if (ignoreCode) {
      return res.body.body
    }else {
      if (res.body.code == 200) {
        return res.body.body
      }else {
        ELEMENT.Message.error(res.body.message)
        return
      }
    }
  }else {
    return new Promise((resolve, reject) => {
      $ajax({
        url: baseUrl,
        jsonData: [params],
      }).then(res => {
        if (res.code != 200) {
          if (!ignoreCode) ELEMENT.Message.error(res.message)
          reject(res)
          return
        }
        let response = res.body
        if (ignoreCode) {
          resolve(response)
        }else {
          if (response.code == 200) {
            resolve(response)
          }else {
            ELEMENT.Message.error(response.message)
            reject(res)
          }
        }
      }).fail(err => {
        ELEMENT.Message.error(err.message || err.msg)
        reject(err)
      })
    })
  }
}
